home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include "global.h"
- #include "mbuf.h"
- #include "internet.h"
- #include "iface.h"
- #include "mac.h"
- #include "cmdparse.h"
- #include <ctype.h>
-
- #include "DeviceMgr.h"
- #include "WindowMgr.h"
- #include "EventMgr.h"
- #include "SerialDvr.h"
- #include "HFS.h"
- #include <time.h>
- extern long timezone;
- extern errno;
- extern struct RemoveIt Head;
- extern char startup[];
- extern char userfile[];
- extern char hosts[];
- extern char mailspool[];
- extern char mailqdir[];
- extern char mailqueue[];
- extern char baddir[];
- extern char bmrc[];
-
- char MAC_drive[1024];
- char *CtoPstr();
- /*
- * dir: Create a directory listing in a temp file and return the resulting file
- * descriptor. If full == 1, give a full listing; else if full ==3, output to console,
- * else return just a list
- * of names.
- *
- */
- FILE *
- dir(path,full)
- char *path;
- int full;
- {
- WDPBRec Mydisk;
- FILE *fp, *fp1;
- CInfoPBRec Everything;
- OSErr e;
- char working_vol[255];
- char working_dir[255];
- char holding_file[255];
- char *PtoCstr(), *ctime();
- char *ptr;
- char buff[255];
-
-
- bzero(&Mydisk, sizeof(WDPBRec));
- bzero(&Everything, sizeof(Everything));
-
- errno = 0;
- if ( full < 3)
- {
- if ( ( fp = fopen("dir.temp", "w")) == NULL)
- {
- printf("Open failed, errno = %d\n",errno);
- return((FILE *)NULL);
- }
- }
- else
- {
- fp = stdout;
- }
-
- Mydisk.ioWDProcID = 0L;
- Mydisk.ioWDDirID = 0L;
- Mydisk.ioVRefNum = 0;
- Mydisk.ioWDVRefNum = 0;
- Mydisk.ioNamePtr = (StringPtr)working_vol;
- if ( path[0] == '\0' )
- {
- path = ":";
- }
- MoveIt(Mydisk.ioNamePtr, path);
-
- e = PBOpenWD( &Mydisk, FALSE);
- if ( e != noErr )
- {
- if ( ( fp1 = fopen(path, "r")) == NULL)
- {
- fclose(fp);
- return((FILE *)NULL);
- }
- else
- {
- fclose(fp1);
- if ( full == 0 )
- {
- ptr = rindex(path, ':');
- ptr++;
- fprintf(fp,"%s\n", ptr);
- fclose(fp);
- fp = fopen("dir.temp", "r");
- }
- else
- {
- sprintf(holding_file, "%s", path);
- CtoPstr(holding_file);
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = Mydisk.ioVRefNum;
- Everything.hFileInfo.ioFDirIndex = 0;
- Everything.hFileInfo.ioDirID = 0L;
- Everything.hFileInfo.ioCompletion = 0;
-
- if ( full == 3 )
- {
- fclose(fp);
- fp = stdout;
- }
-
- if ( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr)
- {
- ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
- ptr +=3;
- ptr[strlen(ptr)-9] = '\0';
- fprintf(fp,"%c %7ld %s %s\n",
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
- Everything.hFileInfo.ioFlLgLen +
- Everything.hFileInfo.ioFlRLgLen, ptr,
- PtoCstr(Everything.hFileInfo.ioNamePtr));
- }
- else
- {
- printf("dir: found error: %d\n", e);
- }
- }
- if ( full < 3 )
- {
- fclose(fp);
- fp = fopen("dir.temp", "r");
- }
- return(fp);
- }
- }
-
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = Mydisk.ioVRefNum;
- sprintf(Everything.hFileInfo.ioNamePtr, "\p");
- Everything.hFileInfo.ioFDirIndex = 1;
- Everything.hFileInfo.ioDirID = Mydisk.ioWDDirID;
- Everything.hFileInfo.ioCompletion = 0;
-
- while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr)
- {
- if ( e == noErr )
- {
- if( full == 0)
- {
- fprintf(fp, "%s\n",
- PtoCstr(Everything.hFileInfo.ioNamePtr));
- }
- else
- {
-
- ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
- ptr +=3;
- ptr[strlen(ptr)-9] = '\0';
- fprintf(fp,"%c %7ld %s %s\n",
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
- Everything.hFileInfo.ioFlLgLen +
- Everything.hFileInfo.ioFlRLgLen, ptr,
- PtoCstr(Everything.hFileInfo.ioNamePtr));
- }
-
- Everything.hFileInfo.ioFlLgLen = 0;
- Everything.hFileInfo.ioFDirIndex++;
-
- }
- else
- {
- PBCloseWD(&Mydisk, FALSE);
- if ( full < 3 )
- {
- fclose(fp);
- fp = fopen("dir.temp", "r");
- unlink("dir.temp");
- return(fp);
- }
- }
- Everything.hFileInfo.ioDirID = Mydisk.ioWDDirID;
- }
- PBCloseWD(&Mydisk, FALSE);
- if ( full < 3)
- {
- fclose(fp);
- fp = fopen("dir.temp", "r");
- unlink("dir.temp");
- }
- return(fp);
- }
-
- /*
- * MoveIt: this is like a bcopy, but uses pascal string type
- * of data.
- */
-
- MoveIt(to,from)
- char *to, *from;
- {
- int size;
-
- bzero(to, 3);
- to[0] = size = strlen(from);
- if ( size <= 0)
- {
- printf("MoveIt: size <= 0 (%d\n", size);
- return;
- }
- to++;
- while( size-- )
- *to++ = *from++;
- }
-
- /*
- * mktemp: make temporary file and return a pointer to it.
- */
-
- char *mktemp(ff)
- char *ff;
- {
- long tt;
- char *ptr;
- if ( ( ptr = index(ff, 'X') ) == NULL)
- {
- ptr = ff;
- }
- tt = TickCount();
- tt &= 0xffffffff;
- sprintf(ptr, "%ld", tt);
- return(ff);
- }
-
- /*
- * index: find the first char in string that matches arg. LSC does not
- * have this routine.
- */
-
- char *index(str, find)
- char *str;
- char find;
- {
- char *strchr();
-
- return(strchr(str, find));
- }
-
-
- static WDPBRec Mydisk;
- static CInfoPBRec Everything;
- static IsOpen = 0; /* tells me that the file is open */
- static char *name_ptr;
- static char working_dir[255];
- static char holding_file[255];
- static char working_vol[255];
-
- /*
- * filedir: find a file that matches the dest arg
- */
-
- char *filedir(path,value,dest)
- char *path;
- int value;
- char *dest;
- {
-
- FILE *fp;
- OSErr e;
- char *PtoCstr(), *ctime();
- char *ptr;
- char buff[255];
- char *rindex();
- char keep;
-
- dest[0] = '\0';
- if ( IsOpen == 0 && value == 0)
- {
- Mydisk.ioWDProcID = 0L;
- Mydisk.ioWDDirID = 0L;
- Mydisk.ioVRefNum = 0;
- Mydisk.ioWDVRefNum = 0;
- Mydisk.ioNamePtr = (StringPtr)working_vol;
- if ( path[0] == '\0' )
- {
- path = ":";
- }
- if ( (ptr = rindex(path, ':')) != NULL)
- {
- keep = *ptr;
- *ptr = '\0';
- MoveIt(Mydisk.ioNamePtr, path);
- *ptr = keep;
- }
- else
- {
- MoveIt(Mydisk.ioNamePtr, path);
- }
- if ( ( name_ptr = rindex( path, '.')) != NULL)
- {
- name_ptr++;
- }
- else
- {
- name_ptr = "";
- }
- }
-
- e = PBOpenWD( &Mydisk, FALSE);
-
- if ( e != noErr )
- {
- printf("filedir: ERROR in PWOpenWD\n");
- IsOpen = 0;
- return( (char *)-1);
- }
-
- /* mark as open */
-
- IsOpen = 1;
-
- /*
- * set up for getting the info from the directory
- */
-
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = Mydisk.ioVRefNum;
- sprintf(Everything.hFileInfo.ioNamePtr, "\p");
-
- if ( value == 0 )
- {
- Everything.hFileInfo.ioFDirIndex = 1;
- }
-
- Everything.hFileInfo.ioDirID = Mydisk.ioWDDirID;
- Everything.hFileInfo.ioCompletion = 0;
-
-
- while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr)
- {
- if ( e == noErr )
- {
-
- PtoCstr(Everything.hFileInfo.ioNamePtr);
- if ( ( ptr = index( Everything.hFileInfo.ioNamePtr, '.')) != NULL)
- {
- ptr++;
- if ( strncmp(ptr, name_ptr, strlen(name_ptr)) == 0)
- {
- sprintf(dest,"%s", Everything.hFileInfo.ioNamePtr);
- Everything.hFileInfo.ioFDirIndex++;
- PBCloseWD(&Mydisk, FALSE);
- return;
- }
- }
- Everything.hFileInfo.ioFlLgLen = 0;
- Everything.hFileInfo.ioFDirIndex++;
- Everything.hFileInfo.ioDirID = Mydisk.ioWDDirID;
-
- }
- else
- {
- IsOpen = 0;
- PBCloseWD(&Mydisk, FALSE);
- return(0);
- }
- Everything.hFileInfo.ioDirID = Mydisk.ioWDDirID;
- }
- IsOpen = 0;
- PBCloseWD(&Mydisk, FALSE);
- }
-
- /*
- * rindex: find the last char in string which matches arg. LSC does not
- * have this routine.
- */
-
- char *rindex(str, c)
- char *str, c;
- {
- char *strrchr();
- return(strrchr(str, c));
- }
-
- int asy_attach();
- extern struct cmds attab[];
-
- /*
- * eihalt: normally this would wait for an interrupt, but it doesn't here.
- * this will check for the button. If the button has been depressed, the process
- * exits
- */
-
- eihalt()
- {
- if (Button())
- {
- iostop();
- exit(0);
- }
- return(0);
- }
-
- /*
- * kbread: see if a char is available on keyboard, if so go get it.
- */
- kbread()
- {
- int mask;
- int ok;
- char c = -1;
- if( kbhit() ){
- c = getch();
- }
- return(c);
- }
-
- /*
- * clksec: return the amount of time in secs.
- */
-
- int32
- clksec()
- {
- return(time(NULL));
- }
-
- /*
- * tmpfile: create a temporary file. Remember it so we can delete it later because
- * the mac does not allow the file to be deleted when it is open.
- */
-
- FILE *
- tmpfile()
- {
- FILE *tmp;
- char *mktemp();
- char *ptr;
- char *name;
- struct RemoveIt *rptr;
-
- if ( ( ptr = malloc(strlen("SMTPXXXXXX")+1)) == NULLCHAR)
- {
- printf("Could not open SMTPXXXXXX for temp file.\n");
- return(NULL);
- }
- sprintf(ptr, "SMTPXXXXXX");
- name = mktemp(ptr);
- if ( ( tmp = fopen(name, "w+") ) == NULL)
- {
- printf("tmpfile: counld not create temp file. (%s)\n", name);
- return(NULL);
- }
- rptr = &Head;
- while(rptr->next != NULL )
- {
- rptr = rptr->next;
- }
-
- if ( (rptr->next = (struct RemoveIt *)malloc(sizeof (struct RemoveIt)) ) == NULL)
- {
- printf("Could not allocate memory for structure RemoveIt\n");
- return(tmp);
- }
- if ( (rptr->name_ptr = malloc(strlen(name)+1) ) == NULL)
- {
- printf("Could not allocate memory for %s\n", name);
- return(tmp);
- }
-
- sprintf(rptr->name_ptr, "%s", name);
- rptr = rptr->next;
- rptr->next = NULL;
- (void)free(ptr);
- return ( tmp );
- }
-
- /* restore: stub */
- restore()
- {}
-
- /* stxrdy: stub */
- stxrdy()
- {return(1);}
-
- /* disable: stub */
- disable()
- {}
-
- /* memstat: stub */
- memstat()
- {
- return(0);
- }
-
- /* checks the time then ticks and updates ISS */
- static int32 clkval = 0;
- void
- check_time()
- {
- int32 iss();
- int32 clksec();
-
- if(clkval != clksec()){
- clkval = clksec();
- tick();
- (void)iss();
- }
- }
-
- /*
- * Access: check the file for access permission. Some of this has to be faked on
- * a Mac, since it does not have access bits.
- */
-
- access(str, perm)
- char *str;
- int perm;
- {
- FILE *fptr;
- CInfoPBRec paramBlock;
- OSErr e;
- char *CtoPstr();
-
- paramBlock.hFileInfo.ioCompletion = 0;
- paramBlock.hFileInfo.ioVRefNum = 0;
- paramBlock.hFileInfo.ioFDirIndex = 0;
- paramBlock.hFileInfo.ioDirID = 0;
- paramBlock.hFileInfo.ioNamePtr = CtoPstr(str);
-
- /*
- * Get info on file named in ioNamePtr
- */
-
- e = PBGetCatInfo( ¶mBlock, FALSE);
- PtoCstr(str);
-
- /*
- * if there is an error then find out if the file is present. If so
- * see if user wants to create the file
- */
-
- if ( e != noErr )
- {
- if ( ( e == fnfErr) && (perm == 2))
- {
- return(0);
- }
- else
- {
- return(1);
- }
- }
- /*
- * check to see if the file is locked or open. refuse it if it is.
- */
-
- if ( (BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 0)
- || BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 7) )
- && perm == 4 )
- {
- return(1);
- }
- else
- return(0);
-
- }
-
- /*
- * Bzero: zero out a buffer
- */
-
- bzero(str, cnt)
- char *str;
- int cnt;
- {
- while(cnt-- != 0)
- *str++ = '\0';
- }
-
- /*
- * memcmp: compare memory
- */
-
- memcmp(str1, str2, len)
- char *str1, *str2;
- int len;
- {
- return( strncmp(str1, str2, len));
- }
-
- /*
- * memset: set a value in memory
- */
-
- memset(str, value, len)
- char *str;
- int value, len;
- {
- while ( len-- != 0)
- {
- *str++ = value;
- }
- }
-
- /*
- * memcpy: copy from one place to another
- */
-
- memcpy(str1, str2, len)
- char *str1, *str2;
- int len;
- {
- while ( len-- != 0)
- *str1++ = *str2++;
- }
-
- /* List directory to console */
- dodir(argc,argv)
- int argc;
- char *argv[];
- {
- if ( argc > 2)
- dir( argv[1], 3);
- else
- dir(":",3);
- return 0;
- }
-
- /*
- * docd: perform a change directory
- */
-
- docd(argc, argv)
- int argc;
- char *argv[];
- {
- char dirname[128],*getwd();
-
- if(argc > 1){
- if(chdir(argv[1]) == -1){
- printf("Can't change directory\n");
- return 1;
- }
- }
- if(getwd(dirname,0) != NULLCHAR){
- printf("%s\n",dirname);
- }
- return 0;
- }
-
- chdir( path )
- char * path;
- {
- WDPBRec Mydisk;
- int err;
- register char * cp;
- char *index();
- OSErr e;
-
- #ifdef DEBUG
- printf("Entering chdir\n");
- #endif
-
- Mydisk.ioWDProcID = 0L;
- Mydisk.ioWDDirID = 0L;
- Mydisk.ioVRefNum = 0;
- printf("chdir: path = %s\n", path);
- Mydisk.ioWDVRefNum = 0;
- CtoPstr(path);
- Mydisk.ioNamePtr = path;
- #ifdef DEBUG
- printf("chdir: calling PBOpenWD.\n");
- printf("Mydisk.ioVRefNum = %d\n", Mydisk.ioVRefNum);
- #endif
- e = PBOpenWD( &Mydisk, FALSE);
-
- if ( e != noErr )
- {
- PtoCstr(path);
- return(-1);
- }
-
- if ( ( e = PBHSetVol( &Mydisk, FALSE) ) != noErr)
- {
- PtoCstr(path);
- return(-1);
- }
-
- PtoCstr(path);
- return 0;
-
- }
-
- char *getwd()
- {
- return(NULL);
- }
-
- /*
- * doshell: execute a shell for the user
- */
-
- doshell()
- {
- printf("SHELL is not implemented.\n");
- }
-
- char *timez = "CST";
-
- char *getenv(str)
- char *str;
- {
- return(timez);
- }
-
- mkdir(name, mode)
- char *name;
- int mode;
- {
-
- HParamBlockRec paramBlock;
- int e;
-
- paramBlock.fileParam.ioCompletion = 0;
- paramBlock.fileParam.ioNamePtr = CtoPstr(name);
- paramBlock.fileParam.ioDirID = 0L;
- paramBlock.fileParam.ioVRefNum = 0;
-
- if ( ( e = PBDirCreate(¶mBlock, FALSE)) != 0 )
- {
- printf("Could not create directory. error = %d\n", e);
- return(-1);
- }
-
- return(0);
- }
-
- rmdir(name)
- char *name;
- {
- HParamBlockRec paramBlock;
- int e;
-
- paramBlock.fileParam.ioCompletion = 0;
- paramBlock.fileParam.ioNamePtr = CtoPstr(name);
- paramBlock.fileParam.ioFVersNum = 0;
- paramBlock.fileParam.ioVRefNum = 0;
-
- if ( ( e = PBDelete(¶mBlock, FALSE)) != 0 )
- {
- printf("Could not create directory, error = %d\n", e);
- return(-1);
- }
- return(0);
- }
-
-
- mac_files()
- {
- int iovRef, e;
- char *PtoCstr();
- int len;
- /*
- * Now find the Volume name and put it back.
- */
- setmem(MAC_drive,0, sizeof(MAC_drive));
-
- if ( ( e = GetVol(MAC_drive, &iovRef) ) != 0)
- {
- printf("mac_files: could not get the volume name.\n");
- exit(-1);
- }
- else
- {
- (void) PtoCstr(MAC_drive);
- }
-
- check_len(STARTUP);
- sprintf(startup,"%s%s\0", MAC_drive, STARTUP);
- check_len(USERFILE);
- sprintf(userfile,"%s%s", MAC_drive, USERFILE);
- check_len(HOSTS);
- sprintf(hosts,"%s%s", MAC_drive, HOSTS);
- check_len(MAILSPOOL);
- sprintf(mailspool,"%s%s", MAC_drive, MAILSPOOL);
- check_len(MAILQDIR);
- sprintf(mailqdir,"%s%s", MAC_drive, MAILQDIR);
- check_len(MAILQUEUE);
- sprintf(mailqueue,"%s%s", MAC_drive, MAILQUEUE);
- check_len(BADDIR);
- sprintf(baddir,"%s%s", MAC_drive, BADDIR);
- check_len(BMRC);
- sprintf(bmrc,"%s%s", MAC_drive, BMRC);
- check_len(ROUTEQDIR);
- sprintf(bmrc,"%s%s", MAC_drive, BMRC);
- }
-
- check_len(str)
- char *str;
- {
- int len;
-
- len = strlen("This line will be overwritten by the mac stuff. MAX LINE");
- if ( (strlen(MAC_drive) + strlen(str)) > len )
- {
- printf("Pathname to %s (max is %d) is to long, please shorten the path.\n", str, len);
- printf("The pathname you attempted was %s%s\n", MAC_drive, str);
- exit(1);
- }
- }
-
- chktasker()
- {}